/*
 * TableColumnModel.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.gui.elements;

import java.util.ResourceBundle;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;
import org.tiberiumlabs.lailaps.settings.Settings;

/**
 *
 * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class TableColumnModel extends DefaultTableColumnModel implements ColumnPositions {

    private final ResourceBundle bundle = ResourceBundle.getBundle("org/tiberiumlabs/lailaps/resources/messages/table");
    private final String[] columnNames = {"S", 
                                          bundle.getString("field.url"),
                                          bundle.getString("field.filename"), 
                                          bundle.getString("field.path"), 
                                          bundle.getString("field.completed"),
                                          bundle.getString("field.speed"),
                                          bundle.getString("field.fullsize"),
                                          bundle.getString("field.elapsed"),
                                          bundle.getString("field.estimated"),
                                          bundle.getString("field.percent"),
                                          bundle.getString("field.sizeprogress")};
    
    public TableColumnModel() {
        super();
        createColumns();
    }

    private void createColumns() {
        int[] columnsOrder = Settings.getInterfaceSettings().getTableColumnOrder();
        
        if (columnsOrder == null || columnsOrder.length <= 0) {
            // creating default columns:
            addColumn(createColumn(COL_STATUS));
            addColumn(createColumn(COL_URL));
            addColumn(createColumn(COL_FILE));
            addColumn(createColumn(COL_COMPLETED));
            addColumn(createColumn(COL_SPEED));
            addColumn(createColumn(COL_FULLSIZE));
            addColumn(createColumn(COL_PROGRESS));
            addColumn(createColumn(COL_SIZEPROGRESS));
            return;
        }
        for (int i = 0; i < columnsOrder.length; i++) {
            addColumn(createColumn(columnsOrder[i], columnsOrder[++i]));
        }
    }
    
    private TableCellRenderer sizeRenderer = null;
    public TableCellRenderer getSizeRenderer() {
        if (sizeRenderer == null) {
            sizeRenderer = new SizeTableCellRenderer();
        }
        return sizeRenderer;
    }
    
    private TableColumn createColumn(int index) {
        TableColumn col = new TableColumn(index);
        col.setHeaderValue(columnNames[index]);
        switch (index) {
            case COL_STATUS:
                {
                    col.setCellRenderer(new IconTableCellRenderer());
                    col.setResizable(false);
                    col.setMaxWidth(18);
                    col.setPreferredWidth(18);
                    col.setMinWidth(18);
                    break;
                }
            case COL_URL:
                {
                    col.setMinWidth(20);
                    col.setPreferredWidth(330);
                    break;
                }
            case COL_FILE:
                {
                    col.setMinWidth(20);
                    col.setPreferredWidth(180);
                    break;
                }
            case COL_PATH:
                {
                    col.setMinWidth(20);
                    col.setPreferredWidth(180);
                    break;
                }
            case COL_COMPLETED:
                {
                    col.setCellRenderer(getSizeRenderer());
                    col.setMinWidth(30);
                    col.setPreferredWidth(96);
                    col.setMaxWidth(300);
                    break;
                }
            case COL_SPEED:
                {
                    col.setCellRenderer(getSizeRenderer());
                    col.setMinWidth(30);
                    col.setPreferredWidth(96);
                    col.setMaxWidth(300);
                    break;
                }
            case COL_FULLSIZE:
                {
                    col.setCellRenderer(getSizeRenderer());
                    col.setMinWidth(30);
                    col.setPreferredWidth(96);
                    col.setMaxWidth(300);
                    break;
                }
            case COL_TIMEELAPSED:
                {
                    break;
                }
            case COL_TIMEESTIMATED:
                {
                    break;
                }
            case COL_PROGRESS:
                {
                    col.setCellRenderer(new ProgressTableCellRenderer());
                    col.setMinWidth(30);
                    col.setPreferredWidth(96);
                    break;
                }
            case COL_SIZEPROGRESS:
                {
                    col.setCellRenderer(new CompletedTableCellRenderer());
                    col.setMinWidth(20);
                    col.setPreferredWidth(180);
                    break;
                }
        }
        return col;
    }
    
    private TableColumn createColumn(int index, int width) {
        TableColumn col = createColumn(index);
        col.setPreferredWidth(width);
        return col;
    }
    
    public void addColumn(int index) {
        if (getColumnAt(index) == null) {
            addColumn(createColumn(index));
        }
    }
    
    public void removeColumn(int index) {
        TableColumn column = getColumnAt(index);
        if (column != null) {
            removeColumn(column);
        }
        checkForLastColumn();
    }
    
    private TableColumn getColumnAt(int index) {
        for (int i = 0; i < tableColumns.size(); i++) {
            TableColumn col = tableColumns.elementAt(i);
            if (col.getModelIndex() == index) {
                return col;
            }
        }
        return null;
    }
    
    private void checkForLastColumn() {
        if (getColumnCount() < 1) {
            addColumn(createColumn(COL_URL));
        }
    }

    public int[] getColumnsOrder() {
        int arraySize = tableColumns.size()*2;
        int[] columnsOrder = new int[arraySize];
        int arrayPosition = 0;
        for (int i = 0; i < tableColumns.size(); i++) {
            TableColumn col = tableColumns.get(i);
            columnsOrder[arrayPosition] = col.getModelIndex();
            arrayPosition++;
            columnsOrder[arrayPosition] = col.getWidth();
            arrayPosition++;
        }
        return columnsOrder;
    }
    
}